import Vue from 'vue'

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

import '@/styles/index.scss'

import App from './App.vue'
import store from './store'
import router from './routers'

import axios from 'axios'
Vue.config.productionTip = false
Vue.prototype.axios = axios
// axios.defaults.baseURL = 'http://127.0.0.1:3000'
axios.defaults.baseURL = 'http://node.znhtedu.com'

import Router from 'vue-router'
const routerPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error => error)
}

import * as custom from './filters'

Object.keys(custom).forEach(key => {
  Vue.filter(key, custom[key])
})

Vue.use(ElementUI)

import './icons'
import 'normalize.css'

router.beforeEach((to, from, next) => {
  let token = localStorage.user
  if (to.meta.requireAuth) {
    if (token) {
      next()
    } else {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    }
  } else {
    next()
  }
})

import FileSaver from "file-saver";
import XLSX from "xlsx";

// 导出表格
Vue.prototype.exportExcel = function (val) {
  this.pageSize = val.length;
  this.currentPage = 1;
  this.$nextTick(() => {
    // 从表生成工作簿对象
    let wb = XLSX.utils.table_to_book(document.querySelector("#out-table"));
    // 获取二进制字符串作为输出
    let wbout = XLSX.write(wb, {
      bookType: "xlsx",
      bookSST: true,
      type: "array"
    });
    try {
      FileSaver.saveAs(
        new Blob([wbout], { type: "application/octet-stream" }),
        // 设置导出文件名称
        "sheetjs.xlsx"
      );
    } catch (e) {
      if (typeof console !== "undefined") console.log(e, wbout);
    }
    this.pageSize = 10;
    return wbout;
  });
},


  new Vue({
    router,
    store,
    render: h => h(App)
  }).$mount('#app')
